App({
  data: {
    //servser:"http://192.168.3.9/shared_clubs/index/"
    servser: "https://wxa.chaojisales.com/Index/"
  },
  onLaunch: function() {
      var that = this;
      var url  = this.data.servser + '/login/index'
      var rawData = '';
  
      wx.login({
          success: function (res) {
              console.log(res);
              if (res.code) {
                  //发起网络请求
                  wx.request({
                      url: url,
                      data: {
                        code: res.code,
                      },
                      success: function (res) {
                          var session_id = res.data.session_id;
                          //保存用户的信息与session_id;
                          wx.setStorage({
                             key: 'session_id',
                             data: session_id
                          });
                          
                          wx.setStorage({
                             key: 'money',
                             data: res.data.user_info.money
                          });

                          wx.setStorage({
                              key: 'mobile',
                              data: res.data.user_info.mobile
                          });

                          //如果用户正在使用球杆
                          if (res.data.ball_arm_number !=0){
                            wx.setStorage({ key: 'ball_arm_number', data: res.data.ball_arm_number });
                          }
                          that.getRedPacketInfo(session_id);

                          //获取用户的微信基本信息
                          wx.getUserInfo({
                              success: function (res) {
                                  rawData = res['rawData'];
                                  wx.request({
                                    url: that.data.servser + '/login/addUserInfo',
                                    data: {
                                      session_id : session_id,
                                      rawData    : rawData,
                                    },
                                    success: function (res) {
                                  
                                    }
                                  })
                              }
                          })
                      }
                  })
              }
          }
    });
  },

  getRedPacketInfo: function (session_id) {
    var that = this;
    wx.request({
      url: that.data.servser + '/index/userRedpacket',
      data: {
        session_id:session_id
      },
      success: function (res) {

        wx.setStorage({
          key: 'redpacket_info',
          data: res.data
        });

        wx.setStorage({
          key: 'not_used_count',
          data: res.data.not_used_count
        });
      }
    })
  },

  globalData: {
    userInfo: null
  }
})
